<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible" append-to-body>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小节名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="小节名称"></el-input>
    </el-form-item>
    <el-form-item label="小节时长">
      <el-col :span="7">
        <el-input-number :min="0" style="width: 100%" v-model="dataForm.hour" placeholder="时"></el-input-number>
      </el-col>
      <el-col  :span="7">
        <el-input-number :min="0" style="width: 100%" v-model="dataForm.minutes" placeholder="分"></el-input-number>
      </el-col>
      <el-col :span="7">
        <el-form-item prop="time">
          <el-input-number :min="0" style="width: 100%" v-model.number="dataForm.second" placeholder="秒"></el-input-number>
        </el-form-item>
      </el-col>
    </el-form-item>
    <el-form-item label="视频地址" prop="url">
      <el-input disabled v-model="dataForm.url" placeholder="视频地址"></el-input>
    </el-form-item>
    <el-form-item label="上传视频">
      <el-button @click="upload()">上传视频</el-button>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    <!-- 弹窗, 上传文件 -->
    <upload v-on:getFileUrl="setFileUrl" v-if="uploadVisible" ref="upload"></upload>
  </el-dialog>
</template>

<script>
  import Upload from "../oss/oss-upload"
  export default {
    components:{
      Upload
    },
    data () {
      return {
        visible: false,
        uploadVisible: false,
        dataForm: {
          id: 0,
          courseId: '',
          name: '',
          time: '',
          createTime: '',
          url: '',
          hour: '',
          minutes: '',
          second: 0
        },
        dataRule: {
          courseId: [
            { required: true, message: '课程id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '小节名称不能为空', trigger: 'blur' }
          ],
          time: [
            {required: false, message: '视频时长不能为空',  trigger: 'blur' }
          ],
          url: [
            { required: true, message: '视频地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id,courseId) {
        this.dataForm.id = id || 0
        this.dataForm.courseId = courseId
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/coursenode/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.courseId = data.courseNode.courseId
                this.dataForm.name = data.courseNode.name
                this.dataForm.time = data.courseNode.time
                this.dataForm.createTime = data.courseNode.createTime
                this.dataForm.url = data.courseNode.url
              }
            })
          }
        })
      },
      setFileUrl(val){
        this.dataForm.url = val
      },
      upload(){
       this.uploadVisible = true
       this.$nextTick(() => {
         this.$refs.upload.init()
       })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.time = this.dataForm.hour * 3600 + this.dataForm.minutes * 60 + this.dataForm.second

            if(this.dataForm.time === 0 ){
              this.$message.error('视频时长不能为0')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/sys/coursenode/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'courseId': this.dataForm.courseId,
                'name': this.dataForm.name,
                'time': this.dataForm.time,
                'createTime': this.dataForm.createTime,
                'url': this.dataForm.url,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
